#include<iostream>
#include<queue>
using namespace std;
int main()
{
	int t;
	cin >> t;
	int** a = new int* [t];
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		a[i] = new int[n + 1];
		a[i][0] = n;
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
		}
	}
	int* arr = new int[t];
	for (int i = 0; i < t; i++) {
		arr[i] = -1;
	}
	int k = 0;
	queue<int>* p = new queue<int>[t];
	string str;
	cin >> str;
	while (str != "STOP") {
		if (str == "ENQUEUE") {
			int n;
			cin >> n;
			for (int i = 0; i < t; i++) {
				for (int j = 1; j <= a[i][0]; j++) {
					if (n == a[i][j]) {
						int l = 0;
						for (int q = 0; q < t; q++) {
							if (arr[q] == i) {
								p[q].push(a[i][j]);
								l = 1;
								break;
							}
						}
						if (l == 0) {
							arr[k] = i;
							p[k].push(a[i][j]);
							k++;
						}
						break;
					}
				}
			}
		}
		else {
			for (int i = 0; i < t; i++) {
				if (!p[i].empty()) {
					int m = p[i].front();
					p[i].pop();
					cout << m << ' ';
					break;
				}
			}
		}
		cin >> str;
	}
	return 0;
}